package org.jwos.platform.ajax;

import java.beans.PropertyDescriptor;

import org.apache.commons.beanutils.PropertyUtils;

public class JSONClassGenerator {

	public static String generateJavaScriptClass(String className) {

		try {
			Class clazz = Class.forName(className);
			String ns = "Ext.namespace(\"" + clazz.getPackage().getName() + "\");\n";
			String cd = className + " = function() {\n";
			String cn = "\n   this._javaClass =\"" + className + "\";\n}";

			String pd = "";
			PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(clazz);
			for (int i = 0; i < pds.length; i++) {
				if (!pds[i].getName().equals("class"))
					pd += "   this." + pds[i].getName() + " = null;\n";
			}

			return ns + cd + pd + cn;
		} catch (Exception ex) {
			ex.printStackTrace();
			return "";
		}
	}

	public static void main(String[] args) {
		String className = "org.jwos.platform.ajax.JSONResponse";
		System.out.println(generateJavaScriptClass(className));
	}
}